use super::*;
use clap::Subcommand;
use const_format::formatcp;

mod conf;
pub mod config;
pub mod cos_move;
pub mod download;
pub mod upload;

use conf::CosConf;
use config::ConfigCmd;
use cos_move::MoveCmd;
use download::DownloadCmd;
use upload::UploadCmd;

const COS_BIN: &str = "coscmd";

pub static CMD_CHECK_COS: PrequireCmd = PrequireCmd::new(
  COS_BIN,
  "--version",
  Some(Link::new(
    "安装 COSCMD",
    "https://cloud.tencent.com/document/product/436/10976",
  )),
  Some(formatcp!(
    "Not found {0}!!\nPlease install {0} first.",
    COS_BIN
  )),
);

#[derive(Debug, Subcommand)]
pub enum CosComands {
  Check,
  // 有时间补上对bucket和region的value校验
  Config {
    #[arg(short = 'a', long)]
    secret_id: String,
    #[arg(short = 's', long)]
    secret_key: String,
    #[arg(short = 'b', long)]
    bucket: String,
    #[arg(short = 'r', long)]
    region: String,
  },
  Download {
    #[arg(short, long, default_value_t = false)]
    force: bool,
    cos_path: String,
    local_path: String,
  },
  Move {
    source_cos_path: String,
    target_cos_path: String,
  },
  Upload {
    #[arg(short, long, default_value_t = false)]
    force: bool,

    local_path: String,
    cos_path: String,
  },
}

pub fn serve_cos(command: &CosComands) {
  match command {
    CosComands::Check => {
      if let Err(err) = CosConf::read_from_default() {
        eprintln!("check config failed:{}", err);
      } else {
        println!("check config success!");
      }
    }
    CosComands::Config {
      secret_id,
      secret_key,
      bucket,
      region,
    } => {
      println!("config coscmd ...");
      // println!(
      //   "secret_id=>{}, secret_key=>{}, bucket=>{}, region={}",
      //   secret_id, secret_key, bucket, region
      // );

      ConfigCmd::new(secret_id, secret_key, bucket, region).exec();
    }
    CosComands::Download {
      force,
      cos_path,
      local_path,
    } => {
      println!("forward coscmd download files to local...");
      // println!(
      //   "force=>{}, cos_path=>{}, local_path=>{}",
      //   force, cos_path, local_path
      // );
      if let Err(err) = DownloadCmd::new(*force, cos_path, local_path).exec() {
        eprintln!("upload failed:{}", err);
      }
    }
    CosComands::Move {
      source_cos_path,
      target_cos_path,
    } => {
      println!("forward coscmd move files to ...");
      // println!(
      //   "local_path=>{}, cos_path=>{}",
      //   local_path, cos_path
      // );

      match CosConf::read_from_default() {
        Ok(conf) => {
          let source_path = format!(
            "{}.cos.{}.myqcloud.com{}",
            &conf.bucket, &conf.region, source_cos_path
          );
          if let Err(err) = MoveCmd::new(&source_path, target_cos_path).exec() {
            eprintln!("move failed:{}", err);
          }
        }
        Err(err) => {
          eprintln!("read config failed:{}", err);
        }
      }
    }
    CosComands::Upload {
      force,
      local_path,
      cos_path,
    } => {
      println!("forward coscmd upload files to bucket...");
      // println!(
      //   "force=>{}, local_path=>{}, cos_path=>{}",
      //   force, local_path, cos_path
      // );
      if let Err(err) = UploadCmd::new(*force, local_path, cos_path).exec() {
        eprintln!("upload failed:{}", err);
      }
    }
  }
}
